package org.dromara.germbank.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.germbank.domain.bo.BusOperationsKnowledgeBo;
import org.dromara.germbank.domain.bo.BusPartnerClinicBo;
import org.dromara.germbank.domain.vo.BusPartnerClinicVo;
import org.dromara.germbank.service.BusPartnerClinicService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 合作诊所管理
 *
 * @author congxing
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/clinics")
public class BusPartnerClinicController extends BaseController {

    private final BusPartnerClinicService busPartnerClinicService;

    /**
     * 查询合作诊所管理列表
     */

    @SaCheckPermission("partners:clinics:list")
    @GetMapping("/list")
    public TableDataInfo<BusPartnerClinicVo> list(BusPartnerClinicBo bo, PageQuery pageQuery) {
        return busPartnerClinicService.queryPageList(bo,pageQuery);
    }

    /**
     * 新增诊所
     */
    @SaCheckPermission("germbank:advertisement:add")
    @Log(title = "诊所管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody BusPartnerClinicBo bo) {
        if (!busPartnerClinicService.checkClinicNameUnique(bo)) {
            return R.fail("新增诊所'" + bo.getClinicName() + "'失败，诊所名称已存在");
        }
        return toAjax(busPartnerClinicService.insertClinic(bo));
    }

    /**
     * 修改保存诊所
     */
    @SaCheckPermission("germbank:advertisement:edit")
    @Log(title = "诊所数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody BusPartnerClinicBo bo) {
        if (!busPartnerClinicService.checkClinicNameUnique(bo)) {
            return R.fail("修改诊所数据'" + bo.getClinicName() + "'失败，诊所已存在");
        }
        busPartnerClinicService.updateClinicData(bo);
        return R.ok();
    }


    /**
     * 删除诊所类型
     *
     * @param clinicCodes      * 删除诊所类型code串
     */
    @SaCheckPermission("germbank:advertisement:remove")
    @Log(title = "诊所", businessType = BusinessType.DELETE)
    @DeleteMapping("/{clinicCodes}")
    public R<Void> remove(@PathVariable Long[] clinicCodes) {
        busPartnerClinicService.deleteClinicDataByIds(Arrays.asList(clinicCodes));
        return R.ok();
    }

}
